/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.landmark;

import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author computer
 */
public class TagCat extends SwingWorker<Boolean, Boolean> {

    FileWriter fw = null;
    FileWriter errorFile = null;
    int pageNo;
    StringBuffer sb = new StringBuffer();
    int count = 0;
    Hashtable<String, String> htModelLinks = new Hashtable<String, String>();
    String status = "";
    JTextArea txtArea;

    @Override
    protected Boolean doInBackground() throws Exception {
        start();
        return true;
    }

    @Override
    protected void done() {
        Util.taskCompleteMsg(status);
    }

    public void start() {
        try {
            String dirName = "./outputs";

            Util.createDirectory(dirName);

            txtArea = Util.createOutputFile("TAG");
            HelperBean helperBean = Util.getHelperBean();

            fw = new FileWriter(helperBean.getOutputFile());
            errorFile = new FileWriter(helperBean.getErrorFile());

            // Here we get all the search pages from page 0 to 9 which we know by seeing the site
            for (pageNo = 1; pageNo <= 21; pageNo++) {
                if (Util.getHelperBean().isTerminateCurrentProcess()) {
                    break;
                }
                String str = "http://www.tacticalassaultgearstore.com/sitemap.aspx?page=" + pageNo;
                txtArea.setText(str);
                txtArea.paintImmediately(txtArea.getVisibleRect());
                processCategoryPage(str);
            }

            txtArea.append("\nFiles are located at : \n" + helperBean.getOutputFile().getAbsolutePath() + "\n" + helperBean.getErrorFile().getAbsolutePath() + "\n");
            txtArea.append("Total products : " + htModelLinks.size() + "\n");
            txtArea.paintImmediately(txtArea.getVisibleRect());

            Enumeration<String> keys = htModelLinks.keys();

            iterateCollection(htModelLinks);

            if (fw != null) {
                fw.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (Exception e1) {
                // TODO: handle exception
            }
        }
        status = "Done processing.";
        if (Util.getHelperBean().isTerminateCurrentProcess()) {
            status = "Process Terminated";
        }
        txtArea.append("\n" + status);
    }

    /**
     *
     * @param htLinks
     */
    private void iterateCollection(Hashtable<String, String> htLinks) {
        try {
            Enumeration<String> keys = htLinks.keys();
            while (keys.hasMoreElements()) {
                if (Util.getHelperBean().isTerminateCurrentProcess()) {
                    break;
                }
                String key = (String) keys.nextElement();
                txtArea.append("Processing page: " + key + "\n");
                txtArea.paintImmediately(txtArea.getVisibleRect());
                sb = new StringBuffer();
                fw.write(key + "\n");
                processModels(htLinks.get(key).toString());
                fw.write(sb.toString().replaceAll("\n", "").replaceAll("\r", "").replaceAll("    ", ""));
                fw.write("\n");
                fw.flush();
            }
        } catch (Exception e) {
        }
    }

    /**
     *
     * @param aUrl
     */
    private void processModels(String aUrl) {
        try {

            Parser parser = Util.getParserFromUrl(aUrl, null);

//            // This call is to get the models available in the model page which are linked to this model like "See also"
            //processCategoryPage(aUrl, true);
            // to know which URL/model we have the data for in the final output/file

            NodeList nl = parser.parse(null);
            NodeList prdtNameList = nl.extractAllNodesThatMatch(new HasAttributeFilter("class", "prod-name"), true);
            sb.append("Product Name: " );
            sb.append( prdtNameList.elementAt(0).toPlainTextString());
            sb.append("\t\t");
            NodeList tableNodes = nl.extractAllNodesThatMatch(new HasAttributeFilter("class", "bg-dark"), true);

            Node targetTableNode = tableNodes.elementAt(0);// retrives the table content where the description starts which we are intersetd in.			

            iterateTag(targetTableNode);
            
            

        } catch (Exception e) {
            try {
                errorFile.write(aUrl + " \n" + e.getMessage() + " \n");
                errorFile.flush();
                //e.printStackTrace();
            } catch (Exception e1) {
                // TODO: handle exception
            }
        }
    }

    /**
     *
     * @param aNodeList
     */
    private void iterateTag(Node aNodeList) {
        if (aNodeList instanceof TextNode) {
            TextNode text = (TextNode) aNodeList;
            sb.append(text.getText());
        } else if (aNodeList instanceof TagNode) {

            // downcast to TagNode
            TagNode tag = (TagNode) aNodeList;

            if (tag.getRawTagName().equalsIgnoreCase("h3")) {
                sb.append("<h1>");
            }
            if (tag.getRawTagName().equalsIgnoreCase("span")) {
                sb.append("<span>");
            }
            if (tag.getRawTagName().equalsIgnoreCase("br")) {
                sb.append("<br>");
            }
            // process recursively (nodes within nodes) via getChildren()				
            NodeList nl = tag.getChildren();
            if (null != nl) {
                try {
                    for (NodeIterator i = nl.elements(); i.hasMoreNodes();) {
                        iterateTag(i.nextNode());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void processCategoryPage(String aUrl) {
        try {
            Document doc = Jsoup.connect(aUrl).get();

            Elements elements = doc.select("td.content-body").select("a[href]");

            for (Element element : elements) {

                String absoluteUrl = element.attr("abs:href");

                htModelLinks.put(absoluteUrl, absoluteUrl);
            }
        } catch (Exception e) {
            try {
                errorFile.write(aUrl + " \n" + e.getMessage() + " \n");
                errorFile.flush();
                e.printStackTrace();
            } catch (Exception e1) {
                // TODO: handle exception
            }
            e.printStackTrace();
        }
    }
}
